<?php

include_once './app/model/transactionModel.php' ;
include_once './app/model/typeModel.php' ;

class accountModel extends Model
{

	public static function getById ( $id )
	{

		$db = &DB::getInstance ( require './config/db_config.php' ) ;

		$params = array (
				'id' => array (
						'value' => intval ( $id ),
						'type' => PDO::PARAM_INT
				)
		) ;
			
		$db->query ( 'SELECT * FROM compte WHERE cmp_id=:id', $params ) ;
			
		$result = $db->fetchAll ( ) ;

		if ( count ( $result ) === 0 )
			return null ;
		else
			return new accountModel ( $result[0], true ) ;

	}
	
	public static function getByOwner ( $id )
	{
	
		$db = &DB::getInstance ( require './config/db_config.php' ) ;
	
		$params = array (
				'id' => array (
						'value' => intval ( $id ),
						'type' => PDO::PARAM_INT
				)
		) ;
			
		$db->query ( 'SELECT * FROM compte WHERE cli_id=:id', $params ) ;
			
		$result = $db->fetchAll ( ) ;
	
		if ( count ( $result ) === 0 )
			return null ;
		else
		{
			
			$res = array ( ) ;
			
			foreach ( $result as $account )
			{
				
				$res[] = new accountModel ( $account, true ) ;
				
			}
			
			return $res ;
			
		}
	
	}
	
	public static function getByRIB ( $cashdeskcode, $account_number, $ribkey )
	{
		
		$db = &DB::getInstance ( require './config/db_config.php' ) ;
		
		$params = array (
				'cashdeskcode' => array (
						'value' => strval ( $cashdeskcode ),
						'type' => PDO::PARAM_STR
				),
				'account_number' => array (
						'value' => strval ( $account_number ),
						'type' => PDO::PARAM_STR
				),
				'ribkey' => array (
						'value' => strval ( $ribkey ),
						'type' => PDO::PARAM_STR
				)
		) ;
			
		$db->query ( 'SELECT * FROM compte WHERE cmp_code_guichet=:cashdeskcode AND cmp_numero_compte=:account_number AND cmp_cle_rib=:ribkey', $params ) ;
			
		$result = $db->fetchAll ( ) ;
		
		if ( count ( $result ) === 0 )
			return null ;
		else
			return new accountModel ( $result[0], true ) ;
		
	}

	public function save ( )
	{
		
		if ( ! isset ( $this->data['cmp_date_creation'] ) && ! isset ( $this->data['cmp_code_guichet'] ) && ! isset ( $this->data['cmp_numero_compte'] ) && ! isset ( $this->data['cmp_cle_rib'] ) && ! isset ( $this->data['cli_id'] ) && ! isset ( $this->data['type_id'] ) )
			throw new Exception ( 'Tous les champs du modèle ne sont pas renseignés.' ) ;
		elseif ( ! $this->already_saved )
		{
				
			$params = array (
					'creation_date' => array (
							'value' => strval ( $this->data['cmp_date_creation'] ),
							'type' => PDO::PARAM_STR
					),
					'cash_desk_code' => array (
							'value' => strval ( $this->data['cmp_code_guichet'] ),
							'type' => PDO::PARAM_STR
					),
					'account_number' => array (
							'value' => strval ( $this->data['cmp_numero_compte'] ),
							'type' => PDO::PARAM_STR
					),
					'rib_key' => array (
							'value' => strval ( $this->data['cmp_cle_rib'] ),
							'type' => PDO::PARAM_STR
					),
					'client_id' => array (
							'value' => intval ( $this->data['cli_id'] ),
							'type' => PDO::PARAM_INT
					),
					'type_id' => array (
							'value' => intval ( $this->data['type_id'] ),
							'type' => PDO::PARAM_INT
					)
			) ;

			$this->db->query ( 'INSERT INTO Compte (cmp_date_creation, cmp_code_guichet, cmp_numero_compte, cmp_cle_rib, cli_id, type_id) VALUES ( :creation_date, :cash_desk_code, :account_number, :rib_key, :client_id, :type_id )', $params ) ;
			$this->data['cmp_id'] = $this->db->lastId ( ) ;
				
			$this->already_saved = true ;
			$this->modified = false ;
				
		}
		else if ( $this->modified )
		{
				
			$params = array (
					'id' => array (
							'value' => intval ( $this->data['cmp_id'] ),
							'type' => PDO::PARAM_INT
					),
					'creation_date' => array (
							'value' => strval ( $this->data['cmp_date_creation'] ),
							'type' => PDO::PARAM_STR
					),
					'cash_desk_code' => array (
							'value' => strval ( $this->data['cmp_code_guichet'] ),
							'type' => PDO::PARAM_STR
					),
					'account_number' => array (
							'value' => strval ( $this->data['cmp_numero_compte'] ),
							'type' => PDO::PARAM_STR
					),
					'rib_key' => array (
							'value' => strval ( $this->data['cmp_cle_rib'] ),
							'type' => PDO::PARAM_STR
					),
					'client_id' => array (
							'value' => intval ( $this->data['cli_id'] ),
							'type' => PDO::PARAM_INT
					),
					'type_id' => array (
							'value' => intval ( $this->data['type_id'] ),
							'type' => PDO::PARAM_INT
					)
			) ;
				
			$this->db->query ( 'UPDATE Compte SET cmp_date_creation=:creation_date, cmp_code_guichet=:cash_desk_code, cmp_numero_compte=:account_number, cmp_cle_rib=:rib_key, cli_id=:client_id, type_id=:type_id WHERE cmp_id=:id', $params ) ;
				
			$this->modified = false ;
				
		}

	}

	public function delete ( )
	{

		if ( $this->already_saved )
		{
			
			$transactions = $this->getTransactions ( ) ;
			
			if ( $transactions != null )
				foreach ( $transactions as $transaction )
				{
				
					$transaction->delete ( ) ;
				
				}
			
			unset ( $transactions ) ;
			
			$params = array (
					'id' => array (
							'value' => intval ( $this->data['cmp_id'] ),
							'type' => PDO::PARAM_INT
					)
			) ;
			
			$this->db->query ( 'DELETE FROM Compte WHERE cmp_id=:id', $params ) ;
			
			$this->already_saved = false ;
			
		}

	}

	public function getId ( ) { return $this->data['cmp_id'] ; }
	public function getCreationDate ( ) { return $this->data['cmp_date_creation'] ; }
	public function getCashDeskCode ( ) { return $this->data['cmp_code_guichet'] ; }
	public function getAccountNumber ( ) { return $this->data['cmp_numero_compte'] ; }
	public function getRIBKey ( ) { return $this->data['cmp_cle_rib'] ; }
	public function getOwner ( ) { return $this->data['cli_id'] ; }
	public function getType ( ) { return $this->data['type_id'] ; }
	
	public function setCreationDate ( $arg ) { $this->data['cmp_date_creation'] = strval ( $arg ) ; $this->modified = true ; }
	public function setgetCashDeskCode ( $arg ) { $this->data['cmp_code_guichet'] = strval ( $arg ) ; $this->modified = true ; }
	public function setAccountNumber ( $arg ) { $this->data['cmp_numero_compte'] = strval ( $arg ) ; $this->modified = true ; }
	public function setRIBKey ( $arg ) { $this->data['cmp_cle_rib'] = strval ( $arg ) ; $this->modified = true ; }
	public function setOwner ( $arg ) { $this->data['cli_id'] = strval ( $arg ) ; $this->modified = true ; }
	public function setType ( $arg ) { $this->data['type_id'] = strval ( $arg ) ; $this->modified = true ; }
	
	public function getTransactions ( )
	{
	
		if ( ! isset ( $this->data['cmp_id'] ) )
			return null ;
		else
			return transactionModel::getByAccount ( $this->data['cmp_id'] ) ;
	}
	
	public function addTransactions ( $transactions = array ( ) )
	{
	
		if ( ! isset ( $this->data['cmp_id'] ) )
			throw new Exception ( 'Le modèle n\'a pas encore été enregisté un fois et n\'a donc pas d\'id.' ) ;
		elseif ( ! is_array ( $transactions ) )
			throw new Exception ( 'Le paramètre $transactions n\'est pas un tableau.' ) ;
		else
		{
				
			foreach ( $transaction as $transaction )
			{
	
				if ( $transaction instanceof transactionModel )
				{
					
					$transaction->setAccount ( $this->data['cmp_id'] ) ;
					$transaction->save ( ) ;
					
				}
	
			}
				
		}
	
	}
	
	public function removeTransactions ( $transactions_to_rm = array ( ) )
	{
	
		if  ( ! is_array ( $transactions_to_rm ) )
			throw new Exception ( 'Le paramètre $transactions_to_rm n\'est pas un tableau.' ) ;
		else
		{
				
			$my_transactions = getTransactions ( ) ;
				
			foreach ( $my_transactions as $my_transaction )
			{
	
				foreach ( $transactions_to_rm as $transaction_to_rm )
				{
						
					if ( $my_transaction.getId ( ) == $transaction_to_rm.getId ( ) )
						$my_transaction->delete ( ) ;
						
				}
	
			}
				
		}
	
	}
	
	public function getAmount ( )
	{
		
		if ( ! isset ( $this->data['cmp_id'] ) )
			return 0 ;
		else
		{
			
			$params = array (
					'id' => array (
					'value' => intval ( $this->data['cmp_id'] ),
					'type' => PDO::PARAM_INT
				)
			) ;
				
			$this->db->query ( 'SELECT SUM(trs_montant) amount FROM transaction WHERE cmp_id=:id', $params ) ;
				
			$result = $this->db->fetchAll ( ) ;
			
			return ( $result[0]['amount'] != null ) ? $result[0]['amount'] : 0 ;
			
		}
		
	}
	
}

?>